package mws;

import io.netty.channel.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ClientHolder {
    private static final Logger log = LoggerFactory.getLogger(ClientHolder.class);
    private static final Map<Channel, Integer> clientMap = new ConcurrentHashMap<>();
    public static final Integer SYNC = 0;
    public static final Integer ASYNC = 1;
    public static final Integer DEFAULT = 2;

    public static boolean addClient(Channel channel,Integer type){
        if(clientMap.containsKey(channel)){
            return false;
        }
        clientMap.put(channel,type);
        return true;
    }


    public static Integer getClient(Channel channel){
        Integer type = clientMap.get(channel);
        if (type == null){
            log.error("ClientHolder getClient error clientId:{}",type);
        }
        return type;
    }

    public static boolean HavingChannel(Channel channel){
        return clientMap.containsKey(channel);
    }


    public static void removeClient(Channel channel) {
        Integer remove = clientMap.remove(channel);
        if(remove == null){
            log.error("ClientHolder removeClient error clientId:{}",remove);
        }
    }
}
